package com.xgp.order;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.xgp.order.yamls.OrderYaml;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;


@SpringBootApplication
@EnableDiscoveryClient // 开启服务发现功能
@ConfigurationPropertiesScan
@EnableConfigurationProperties(OrderYaml.class)
@EnableFeignClients//开启openfeign的远程调用功能
public class OrderMainApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderMainApplication.class);
    }

//    需求：当配置文件发送变动时，邮件通知给开发者
    //1、项目启动就监听配置文件变化
    //2、发生变化后拿到变化值
    //3、发送邮件
    @Bean
    ApplicationRunner applicationRunner(NacosConfigManager nacosConfigManager) { // 项目一启动就启动的对象
        return new ApplicationRunner() {
            @Override
            public void run(ApplicationArguments args) throws Exception {
//                System.out.println("====================");
                ConfigService configService = nacosConfigManager.getConfigService();
                configService.addListener("service-order.yaml", "DEFAULT_GROUP",
                        new Listener() {
                            @Override
                            public Executor getExecutor() {
                                return Executors.newFixedThreadPool(3);//线程池
                            }

                            @Override
                            public void receiveConfigInfo(String s) {
                                System.out.println("变化的配置信息："+s);
                                System.out.println("邮件通知");
                            }
                        });
            }
        };
    }
}
